package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiCarCanScheduleQueryBo;
import com.piggy.logi.sys.bo.LogiCarrierCarExtEditBo;
import com.piggy.logi.sys.bo.LogiCarrierCarQueryBo;
import com.piggy.logi.sys.bo.LogiDriverCanScheduleQueryBo;
import com.piggy.logi.sys.service.ILogiCarrierCarService;
import com.piggy.logi.sys.vo.LogiCarrierCarExtVo;
import com.piggy.logi.sys.vo.LogiCarrierCarVo;
import com.piggy.logi.sys.vo.LogiCarrierDriverExtVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 承运商车辆信息Controller
 */
@Api(value = "承运商车辆信息控制器", tags = {"承运商车辆信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/logi/carrierCar")
public class LogiCarrierCarController extends BaseController {

    private final ILogiCarrierCarService iLogiCarrierCarService;

    /**
     * 查询承运商车辆信息列表
     */
    @ApiOperation("查询承运商车辆信息列表")
//    @SaCheckPermission("@ss.hasPermi('sys:car:list')")
    @PostMapping("/list")
    public TableDataInfo<LogiCarrierCarExtVo> list(@Validated @RequestBody LogiCarrierCarQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiCarrierCarService.queryPageList(bo);
    }

    /**
     * 导出承运商车辆信息列表
     */
    @ApiOperation("导出承运商车辆信息列表")
//    @SaCheckPermission("@ss.hasPermi('sys:car:export')")
    @LogiLog(title = "承运商车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated @RequestBody LogiCarrierCarQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiCarrierCarExtVo> list = iLogiCarrierCarService.queryList(bo);
        ExcelUtil<LogiCarrierCarExtVo> util = new ExcelUtil<>(LogiCarrierCarExtVo.class);
        util.exportExcel(response, list, "承运商车辆信息");
    }

    /**
     * 获取承运商车辆信息详细信息
     */
    @ApiOperation("获取承运商车辆信息详细信息")
//    @SaCheckPermission("@ss.hasPermi('sys:car:query')")
    @GetMapping("/{id}")
    public R<LogiCarrierCarVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable("id") Long id) {
        return R.ok(iLogiCarrierCarService.queryById(id));
    }

    /**
     * 新增承运商车辆信息
     */
    @ApiOperation("新增承运商车辆信息")
//    @SaCheckPermission("@ss.hasPermi('sys:car:add')")
    @LogiLog(title = "承运商车辆信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiCarrierCarExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiCarrierCarService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改承运商车辆信息
     */
    @ApiOperation("修改承运商车辆信息")
//    @SaCheckPermission("@ss.hasPermi('sys:car:edit')")
    @LogiLog(title = "承运商车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiCarrierCarExtEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiCarrierCarService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除承运商车辆信息
     */
    @ApiOperation("删除承运商车辆信息")
//    @SaCheckPermission("@ss.hasPermi('sys:car:remove')")
    @LogiLog(title = "承运商车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空")
                             @PathVariable Long[] ids) {
        return R.ok(iLogiCarrierCarService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 获取承运商车辆信息详细信息
     */
    @ApiOperation("通过车牌号获取承运商车辆详细信息")
//    @SaCheckPermission("@ss.hasPermi('sys:car:query')")
    @GetMapping("queryByLicensePlate/{licensePlate}")
    public R<LogiCarrierCarExtVo> queryByLicensePlate(@NotNull(message = "车牌号不能为空") @PathVariable("licensePlate") String licensePlate) {
        return R.ok(iLogiCarrierCarService.queryByLicensePlate(SecurityUtils.getMainDeptId(), licensePlate));
    }

    /**
     * 查询可调度的司机列表
     */
    @ApiOperation("查询可调度的车辆列表")
    @PostMapping("/canScheduleList")
    public List<LogiCarrierCarExtVo> canScheduleList(@Validated @RequestBody LogiCarCanScheduleQueryBo bo) {
        bo.setDeptId(SecurityUtils.getMainDeptId());
        return iLogiCarrierCarService.canScheduleList(bo);
    }

}
